//计算利息
export const calculateInterest = (totalAmount, totalPeriods, annualRate, currentPeriod) => {
	if (currentPeriod < 1 || currentPeriod > totalPeriods) {
		throw new Error("当前期数必须在 1 和总期数之间");
	}
	// 每期还款的固定本金
	const principalPerPeriod = totalAmount / totalPeriods;
	// 剩余本金（到当前期前的未偿还本金）
	const remainingPrincipal = totalAmount - principalPerPeriod * (currentPeriod - 1);
	// 月利率
	const monthlyRate = annualRate / 12;
	// 当前期利息 = 剩余本金 * 月利率
	const interest = remainingPrincipal * monthlyRate;
	return Math.round(interest) / 100;
}
//计算每期还款本金
export const divideAndRound = (dividend, divisor, decimalPlaces = 2) => {
	const result = dividend / divisor;
	const factor = Math.pow(10, decimalPlaces); // 用于控制小数位
	return Math.round(result * factor) / factor; // 保留指定小数位
}